import { _decorator, Component, Node, EventTouch, UIOpacity, Widget} from 'cc';
import { Game } from '../core/Game';
import { Databus } from '../core/Databus';
import { EventManager } from '../core/EventManager';
import DataEventReport from '../platform/DataEventReport';
import { NetSoundManager, SoundManager } from '../framework/SoundManager';
import { updateSetting } from '../services/Setting';
const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('GameOperateController')
export class GameOperateController extends Component {
  // 方向面板控制参数
  private gameOperationSwitch: Node = null;
  private gameOperateController: Node = null;
  private gameOperateLeave: Node = null;
  private gameOperateSurrend: Node = null;
  private gameOperateSetting: Node = null;
  private gameSettingController: Node = null;
  private musicSwitch: Node = null;
  private soundEffectSwitch: Node = null;
  private leaveOpacity = 255;
  private surrendOpacity = 50;
  onLoad() {
    this.gameOperationSwitch = this.node.getChildByName('GameOperationSwitch');
    this.gameOperateController = this.node.getChildByName('GameOperateController');

    this.gameOperateLeave = this.node.getChildByName('GameOperateController').getChildByName('OperateLeave');
    this.gameOperateSurrend = this.node.getChildByName('GameOperateController').getChildByName('OperateSurrend');
    this.gameOperateSetting = this.node.getChildByName('GameOperateController').getChildByName('OperateSetting');
    this.gameSettingController = this.node.getChildByName('GameSettingController');
    this.musicSwitch = this.gameSettingController.getChildByName('MusicSwitch');
    this.soundEffectSwitch = this.gameSettingController.getChildByName('SoundEffectSwitch');
    this.gameOperateController.active = false;
    this.gameSettingController.active = false;

    this.musicSwitch.on(Node.EventType.TOUCH_END, this.changeMusicSwitch, this);
    this.soundEffectSwitch.on(Node.EventType.TOUCH_END, this.changeSoundEffect, this);

    this.gameOperationSwitch.on(Node.EventType.TOUCH_END, this.switchOperation, this);

    this.gameOperateLeave.on(Node.EventType.TOUCH_END, this.operateLeave, this);
    this.gameOperateSurrend.on(Node.EventType.TOUCH_END, this.operateSurrend, this);
    this.gameOperateSetting.on(Node.EventType.TOUCH_END, this.operateSetting, this);
    EventManager.Instance().AddEventListener('TouchClickPoint', this, this.touchClickPoint);
    this.aliginMargin();
  }
  aliginMargin() {
    const widget = this.node.getComponent(Widget);
    widget.left = databus.sceenSize.alignMargin();
  }
  start() {
    if (databus.gameSetting.musicOn === 1) {
      this.gameSettingController.getChildByName('MusicSwitch').getChildByName('CheckedSprite').active = true;
    } else {
      this.gameSettingController.getChildByName('MusicSwitch').getChildByName('CheckedSprite').active = false;
    }

    if (databus.gameSetting.soundOn === 1) {
      this.gameSettingController.getChildByName('SoundEffectSwitch').getChildByName('CheckedSprite').active = true;
    } else {
      this.gameSettingController.getChildByName('SoundEffectSwitch').getChildByName('CheckedSprite').active = false;
    }

    if (databus.gameSetting.musicOn === 1) {
      NetSoundManager.Instance().playBgMusic();
    }
  }
  update(deltaTime: number) {
    this.setOpacity();
    const leaveOpacity = this.gameOperateLeave.getComponent(UIOpacity);
    leaveOpacity.opacity = this.leaveOpacity;

    const surrendOpacity = this.gameOperateSurrend.getComponent(UIOpacity);
    surrendOpacity.opacity = this.surrendOpacity;
  }
  public touchClickPoint(name: string, e: EventTouch) {
    this.gameOperateController.active = false;
    this.gameSettingController.active = false;
  }
  switchOperation(e: EventTouch) {
    if (this.gameOperateController.active) {
      this.hideOperateController();
      return;
    }
    this.showOperateController();
  }

  operateLeave() {
    if (!this.canShowLeave()) return;
    this.hideOperateController();
    EventManager.Instance().Emit('EndGame', {});
    DataEventReport.Instance().reportComponentEvent(
      databus.sceneStack.currentPageName(), 'OperateLeve'
    );
  }

  operateSurrend() {
    if (!this.canShowSurrend()) return;
    this.hideOperateController();
    Game.Instance().selfSurrender();
    EventManager.Instance().Emit('EndGame', {});
    DataEventReport.Instance().reportComponentEvent(
      databus.sceneStack.currentPageName(), 'OperateSurrend'
    );
  }

  public changeMusicSwitch() {
    if (databus.gameSetting.musicOn === 1) {
      this.gameSettingController.getChildByName('MusicSwitch').getChildByName('CheckedSprite').active = false;
      databus.gameSetting.musicOn = 0;
      NetSoundManager.Instance().stopBgMusic();
    } else {
      this.gameSettingController.getChildByName('MusicSwitch').getChildByName('CheckedSprite').active = true;
      databus.gameSetting.musicOn = 1;
      NetSoundManager.Instance().playBgMusic();
    }
    this.updateMusicSetting();
  }

  public changeSoundEffect() {
    if (databus.gameSetting.soundOn === 1) {
      this.gameSettingController.getChildByName('SoundEffectSwitch').getChildByName('CheckedSprite').active = false;
      databus.gameSetting.soundOn = 0;
    } else {
      this.gameSettingController.getChildByName('SoundEffectSwitch').getChildByName('CheckedSprite').active = true;
      databus.gameSetting.soundOn = 1;
    }
  }

  public operateSetting() {
    this.showSettingController();
  }

  private showSettingController() {
    this.gameSettingController.active = true;
  }
  private hideSettingController() {
    this.gameSettingController.active = false;
  }
  private canShowLeave(){
    if (Game.Instance().battleCate.isEndlessBattle()) {
      return true;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      return true;
    }
    return false;
  }

  private canShowSurrend(){
    if (Game.Instance().battleCate.isEndlessBattle()) {
      return false;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      return false;
    }
    return true;
  }

  private hideOperateController() {
    this.gameOperateController.active = false;
    this.gameSettingController.active = false;
  }

  private showOperateController() {
    this.gameOperateController.active = true;
  }

  private setOpacity() {
    if (Game.Instance().battleCate.isEndlessBattle()) {
      this.leaveOpacity = 255;
      this.surrendOpacity = 50;
      return;
    }
    if (Game.Instance().battleCate.isFancyBattle()) {
      this.leaveOpacity = 255;
      this.surrendOpacity = 50;
      return;
    }
    this.leaveOpacity = 50;
    this.surrendOpacity = 255;
  }

  private updateMusicSetting() {
    updateSetting(databus.selfInfo.userId, databus.selfInfo.userId, 4, 'musicSetting', `${databus.gameSetting.musicOn}`);
  }
}